package com.panshi.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
@Slf4j
public class LogicInterceptor implements HandlerInterceptor {

    /**
     * 在请求被处理前调用
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //检查每个到来的请求对应的session域中是否有登录标识
        Object logicEmail = request.getSession().getAttribute("logicEmail");
        log.info("logicEmail:  "+logicEmail);
        if(null == logicEmail || !(logicEmail instanceof String)){
            //未登录，重定向到登录页
            response.sendRedirect("/intercept/inter");
            return false;
        }
        String emailName = (String)logicEmail;
        System.out.println("登录的邮箱号："+emailName);

        return true;
    }

    /**
     * 在请求被处理后调用
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 在整个请求结束后调用
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
